package com.sch.www.ele.services.impl;


import com.sch.www.codeGenerator.base.BaseServicesSimpleImp;
import com.sch.www.ele.entity.CareGroup;
import com.sch.www.ele.entity.CareMember;
import com.sch.www.ele.entity.CareMemberExample;
import com.sch.www.ele.entity.CareUser;
import com.sch.www.ele.mapper.CareMemberMapper;
import com.sch.www.ele.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CareMemberServicesImpl extends BaseServicesSimpleImp<CareMember, CareMemberExample, CareMemberMapper, CareMemberServices> implements CareMemberServices {

    @Autowired
    CareGroupServices careGroupServices;
    @Autowired
    CareUnitServices careUnitServices;
    @Autowired
    CareUserServices careUserServices;
    @Autowired
    CareMemberToElevatorServices careMemberToElevatorServices;

    @Override
    protected CareMemberExample getExampleNewInstance() {
        return new CareMemberExample();
    }

    @Override
    protected String getTableName() {
        return null;
    }


    @Override
    public List<CareMember> selectByGroupId(String... groupIds) {
        if (isNull(groupIds)) {
            return new ArrayList<>();
        } else {
            CareMemberExample example = getExampleNewInstance();
            if (groupIds.length == 1) {
                example.createCriteria().andGroupIdEqualTo(groupIds[0]);
            } else {
                example.createCriteria().andGroupIdIn(Arrays.asList(groupIds));
            }
            return getMapper().selectByExample(example);
        }
    }

    @Override
    public List<String> selectUserIdByGroupId(String... groupIds) {
        List<CareMember> groupToUserList = getCurrentProxy().selectByGroupId(groupIds);
        List<String> userIdList = groupToUserList.stream().map(item -> item.getUserId()).collect(Collectors.toList());
        return userIdList;
    }


    @Override
    public List<CareMember> selectByUserId(String... userIds) {
        if (isNull(userIds)) {
            return new ArrayList<>();
        } else {
            CareMemberExample example = getExampleNewInstance();
            if (userIds.length == 1) {
                example.createCriteria().andUserIdEqualTo(userIds[0]);
            } else {
                example.createCriteria().andUserIdIn(Arrays.asList(userIds));
            }
            return getMapper().selectByExample(example);
        }
    }

    @Override
    public List<String> selectGroupIdByUserId(String... userIds) {
        List<CareMember> groupToUserList = getCurrentProxy().selectByUserId(userIds);
        List<String> groupIdList = groupToUserList.stream().map(item -> item.getGroupId()).collect(Collectors.toList());
        return groupIdList;
    }



    @Override
    public int insertSelective(String groupId, String userId) {
        CareGroup careGroup = careGroupServices.selectById(groupId);
        CareUser careUser = careUserServices.selectById(userId);
        CareMember careMember = new CareMember();
        careMember.setUserId(careUser.getId());
        careMember.setUserName(careUser.getName());
        careMember.setUserIdCard(careUser.getIdCard());
        careMember.setGroupId(careGroup.getId());
        careMember.setCareUnitId(careGroup.getCareUnitId());
        return insertSelective(careMember);
    }

    @Override
    public int deleteById(String... ids) {
        careMemberToElevatorServices.deleteByMemberId(ids);
        return super.deleteById(ids);
    }
}

/**
 * @Override public List<String> selectRoleIdByResourceId(String... resourceIds) {
 * if (isNull(resourceIds)) {
 * return new ArrayList<>();
 * } else {
 * ResourceLinkRoleExample example = getExampleNewInstance();
 * if (resourceIds.length == 1) {
 * example.createCriteria().andResourceIdEqualTo(resourceIds[0]);
 * } else {
 * example.createCriteria().andResourceIdIn(Arrays.asList(resourceIds));
 * }
 * List<ResourceLinkRole> resourceLinkRoleList = getMapper().selectByExample(example);
 * List<String> roleIdList = resourceLinkRoleList.stream().map(item -> item.getRoleId()).collect(Collectors.toList());
 * return roleIdList;
 * }
 * }
 * @Override public List<String> selectResourceIdByRoleId(String... roleIds) {
 * if (isNull(roleIds)) {
 * return new ArrayList<>();
 * } else {
 * ResourceLinkRoleExample example = getExampleNewInstance();
 * if (roleIds.length == 1) {
 * example.createCriteria().andRoleIdEqualTo(roleIds[0]);
 * } else {
 * example.createCriteria().andRoleIdIn(Arrays.asList(roleIds));
 * }
 * List<ResourceLinkRole> resourceLinkRoleList = getMapper().selectByExample(example);
 * List<String> resourceIdList = resourceLinkRoleList.stream().map(item -> item.getResourceId()).collect(Collectors.toList());
 * return resourceIdList;
 * }
 * }
 * @Override public int deleteByRoleId(String... roleIds) {
 * if (isNull(roleIds)) {
 * return 0;
 * } else {
 * ResourceLinkRoleExample example = getExampleNewInstance();
 * if (roleIds.length == 1) {
 * example.createCriteria().andRoleIdEqualTo(roleIds[0]);
 * } else {
 * example.createCriteria().andRoleIdIn(Arrays.asList(roleIds));
 * }
 * int result = getMapper().deleteByExample(example);
 * return result;
 * }
 * }
 */